package com.slipper.core.file;

import com.slipper.core.file.vo.FileVO;
import com.slipper.util.Tool;
import jakarta.servlet.http.HttpServletResponse;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public interface IFileService {
    FileVO saveFile(String fileName, byte[] bytes) throws Exception;

    FileVO saveFile(File file) throws Exception;

    byte[] readFile(String path) throws Exception;

    void readFile(String path, String range, HttpServletResponse response) throws Exception;

    void saveFileToPath(String path, byte[] bytes) throws Exception;

    /**
     * 获取文件名
     * @return 文件名
     */
    default String getFileName() {
        return (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date()) + Tool.randomString(6);
    }

    /*
     * 获取文件扩展名
     * @return 文件扩展名
     */
    default String getExtension(String fileName) {
        if (Tool.isBlank(fileName)) return "";

        int i = fileName.lastIndexOf('.');
        if (i > -1 && i < (fileName.length() - 1)) {
            return fileName.substring(i);
        }
        return "";
    }
}
